#include "temperatureSample.h"
#include "adcConvertData.h"
#include "math.h"


#define		REFERENCE_VOLTAGE							3.3			// mcu reference voltage set;
#define 	RESOLUTION_RATIO							(1<<12)		// adc sample resolution
#define 	VOLTAGE_DIVIDER_RESISTOR					30000   	// hard desinger voltage divider reissotr ;
#define		TEMPERATURE_SAMPLE_CHANNLE_MAX				4

const uint16_t tempTable[] = {
	1000,1003,1007,1011,1015,1019,1023,1027,1031,1035,        	//0-9	
	1039,1042,1046,1050,1054,1058,1062,1066,1070,1074,			//10-19
	1077,1018,1085,1089,1093,1097,1101,1105,1108,1112,			//20-29
	1116,1120,1124,1139,1132,1136,1139,1143,1147,1151,			//30-39
	1155,1159,1163,1166,1170,1174,1178,1182,1186,1190,			//40-49
	1193,1197,1201,1205,1209,1213,1217,1220,1224,1228,			//50-59
	1232,1236,1240,1243,1247,1251,1255,1259,1263,1266,			//60-69
	1270,1274,1278,1282,1286,1289,1293,1297,1301,1305,			//70-79
	1308,1312,1316,1320,1324,1328,1331,1335,1339,1343,			//80-89
	1347,1350,1354,1358,1362,1366,1369,1373,1377,1381,			//90-99
	1385														//100
};



void temperatureSampleEntry(void)
{
	uint8_t i;
	uint8_t j;
	uint16_t  getAdcValue[TEMPERATURE_SAMPLE_CHANNLE_MAX];
	uint16_t resistanceValue[TEMPERATURE_SAMPLE_CHANNLE_MAX];	
	uint16_t temperatureVlaue[TEMPERATURE_SAMPLE_CHANNLE_MAX];
	float cvtVoltage[TEMPERATURE_SAMPLE_CHANNLE_MAX];
	
	getAdcValue[0] = getAdcData(AGUN_PTC1000_WIRE);	
	getAdcValue[1] = getAdcData(AGUN_PTC1000_HEAD);	
	getAdcValue[2] = getAdcData(BGUN_PTC1000_WIRE);	
	getAdcValue[3] = getAdcData(BGUN_PTC1000_HEAD);
	
	
	//sample value convert voltage value
	for(i = 0; i < TEMPERATURE_SAMPLE_CHANNLE_MAX; i ++)
	{
		cvtVoltage[i] = getAdcValue[i] * REFERENCE_VOLTAGE / RESOLUTION_RATIO;
	}
	//sample value convert resistance value.
	for(i = 0; i < TEMPERATURE_SAMPLE_CHANNLE_MAX; i ++)
	{
		resistanceValue[i] = (uint16_t)(cvtVoltage[i] * VOLTAGE_DIVIDER_RESISTOR) / (REFERENCE_VOLTAGE - cvtVoltage[i]);
	}
	
	//find table resistance value convert  temperature value.
	for(i = 0; i < TEMPERATURE_SAMPLE_CHANNLE_MAX; i++)
	{
		for(j = 0; j < sizeof(tempTable)/2; j ++)
		{
			if((resistanceValue[i] > tempTable[i])  && (resistanceValue[i] < tempTable[j+1]))
			{
				temperatureVlaue[i] = j;
				break;
			}
		}
	}
	
//	printf("Agun temperture %02d, %02d  Bgun wire temperture %02d  %02d\n",temperatureVlaue[0] , temperatureVlaue[1] ,temperatureVlaue[2] ,temperatureVlaue[3]);
	
	
}
